#!/usr/bin/perl
#
# Copyright 2002-2005 Marc Tremblay
#
# This file is part of ExpireMail.
#
# ExpireMail is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# ExpireMail is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ExpireMail; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# usage: expiremail FOLDER_ROOT DAYS
#
# Calls expirefolder on folders found in FOLDER_ROOT, passing DAYS
#
# $Id$

use Date::EzDate;

unless (@ARGV == 2) {
    die "usage: expiremail <folder root> <days>\n";
}

# calculate the cutoff date
$cutoff = Date::EzDate->new();
$cutoff->{'epochday'} -= $ARGV[1];

&doit($ARGV[0]);

sub doit {
    my($root) = (@_);

    opendir(DIR, $root);
    my(@files) = readdir(DIR);
    closedir(DIR);

    while (@files) {
        my($file) = shift(@files);

        if (($file eq '.') || ($file eq '..') || ($file =~ /inbox/i)) {
            next;
        }

        my($fullpath) = join('/', $root, $file);

        if (-d $fullpath) {
            &doit($fullpath);
        }
        else {
	    	my($output) = `expirefolder "$fullpath" "$cutoff"`;
	   	 	if ($output) {
				print "$fullpath:\n";
				print "$output\n";
				print "\n";
	    	}
        }
    }
}
